package cn.com.jrr.core.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.jrr.core.common.ToolUtils;
import cn.com.jrr.core.common.WebKey;
import cn.com.jrr.core.anotation.SystemControllerLog;
import cn.com.jrr.core.model.DataTableRequest;
import cn.com.jrr.core.model.DataTableResponse;
import cn.com.jrr.core.model.ResponseMessage;
import cn.com.jrr.core.model.VoteDetail;
import cn.com.jrr.core.service.VoteDetailService;

@Controller
@RequestMapping("/votedetail")
public class VoteDetailAction {
    @Autowired
    private VoteDetailService voteDetailService;
    private VoteDetail voteDetail;
    @Autowired
    private ResponseMessage responseMessage;
    private DataTableResponse<VoteDetail> dt;
    /**
     * 初始化
     *
     */

    @RequestMapping("/main")
    public String info(Integer voteid,HttpServletRequest request){
        request.getSession().setAttribute(WebKey.VOTEID, voteid);
        return "/vote/votedetaillist";
    }


    /**
     * 查询全部
     */
    @ResponseBody
    @RequestMapping("/list")
    @SystemControllerLog(description="查询消息")
    public DataTableResponse<VoteDetail> list(DataTableRequest dataTableRequest,HttpServletRequest request){
        Integer voteid=(Integer)request.getSession().getAttribute(WebKey.VOTEID);
        dt=voteDetailService.getAll(dataTableRequest,voteid);
        return dt;
    }
    /**
     * 添加信息
     */
    @ResponseBody
    @RequestMapping("/saveorupdate")
    @Transactional
    @SystemControllerLog(description="添加信息")
    public ResponseMessage saveOrUpadate(VoteDetail voteDetail,HttpServletRequest request){
        if(voteDetail==null){
            responseMessage.setMessage("添加失败 ");
            return responseMessage;
        }
        try{
            voteDetail.setVoteid((Integer)request.getSession().getAttribute(WebKey.VOTEID));
            if(voteDetail.getId()!=null){
                voteDetailService.update(voteDetail);
                responseMessage.setStatus("success");
            }else{

                voteDetailService.add(voteDetail);
                responseMessage.setStatus("success");
            }
        }catch(Exception e){
            responseMessage.setStatus("fail");
        }
        return responseMessage;
    }

    /**
     * 获取一个
     */
    @ResponseBody
    @RequestMapping("/get")
    public VoteDetail get(@RequestParam Integer id){
        voteDetail=voteDetailService.get(id);
        return voteDetail;
    }
    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    public ResponseMessage remove(@RequestParam String idString){
        List<Integer> idList=ToolUtils.splitIdString(idString);
        for(Integer id:idList){
            voteDetailService.delete(id);
        }
        responseMessage.setStatus("success");
        return responseMessage;
    }

    @RequestMapping("/editview")
    public ModelAndView editView(Integer id){
        voteDetail=get(id);
        ModelAndView mv=new ModelAndView();
        mv.setViewName("/vote/votedetailedit");
        mv.addObject("voteDetail", voteDetail);
        return mv;
    }
}
